package com.wondream.myframework.app.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;

public class HyTreeDesUtil {
    public static final byte[] FIX_KEY = "FC5B8717495251443A9A6D90AF59A3A4FC5B871749525144".getBytes();
    public static final byte[] keyiv = {8, 7, 6, 5, 4, 3, 2, 1};


    /**
     * 加密以byte[]明文输入,byte[]密文输出
     *
     * @param byteData
     * @return
     * @throws Exception
     */
    public static byte[] getDesEncCode(byte[] byteKey, byte[] byteData) throws Exception {
        byte[] byteFina = null;
        try {
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = skf.generateSecret(new DESKeySpec(byteKey));

            Cipher cipher;
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom(byteKey));
            byteFina = cipher.doFinal(byteData);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
        return byteFina;
    }

    /**
     * 解密以byte[]密文输入,byte[]明文输出
     *
     * @param
     * @return
     * @throws Exception
     */
    public static byte[] getDesDecCode(byte[] byteKey, byte[] byteData) throws Exception {
        byte[] byteFina = null;
        try {
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = skf.generateSecret(new DESKeySpec(byteKey));

            Cipher cipher;
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom(byteKey));
            byteFina = cipher.doFinal(byteData);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
        return byteFina;
    }

    /**
     * 加密以byte[]明文输入,byte[]密文输出
     *
     * @param byteData
     * @return
     * @throws Exception
     */
    public static byte[] getDes3EncCode(byte[] byteKey, byte[] byteData) throws Exception {
        byte[] byteFina = null;
        try {
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
            SecretKey secretKey = skf.generateSecret(new DESedeKeySpec(byteKey));

            Cipher cipher;
            cipher = Cipher.getInstance("DESede");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byteFina = cipher.doFinal(byteData);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
        return byteFina;
    }

    /**
     * 加密以byte[]明文输入,byte[]密文输出
     *
     * @param byteData
     * @return
     * @throws Exception
     */
    public static byte[] getDes3DecCode(byte[] byteKey, byte[] byteData) throws Exception {
        byte[] byteFina = null;
        try {
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
            SecretKey secretKey = skf.generateSecret(new DESedeKeySpec(byteKey));

            Cipher cipher;
            cipher = Cipher.getInstance("DESede");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byteFina = cipher.doFinal(byteData);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
        return byteFina;
    }



    /**
     * 公共秘钥加密
     *
     * @param str
     * @param logger
     * @return
     */
    public static String getDes3EncodeCBC(String str, Logger logger) {
        byte[] crypt_bytes = null;
        try {
            crypt_bytes = HyTreeDesPKCPaddingUtil.des3EncodeCBC(HyTreeDesPKCPaddingUtil.FIX_KEY_PUBLIC.getBytes(), HyTreeDesPKCPaddingUtil.keyiv, str.getBytes("UTF-8"));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
        return HyBase64.encode(crypt_bytes);
    }

    /**
     * 公共秘钥解密
     *
     * @param str
     * @param logger
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getDes3DecodeCBC(String str, Logger logger) {
        byte[] crypt_bytes = null;
        try {
            crypt_bytes = HyTreeDesPKCPaddingUtil.des3DecodeCBC(HyTreeDesPKCPaddingUtil.FIX_KEY_PUBLIC.getBytes(), HyTreeDesPKCPaddingUtil.keyiv, HyBase64.decode(str));
            return new String(crypt_bytes, "UTF-8");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        //陈剑祥
        //黄南阳
        //张少辉
        //赵培先
        //许榆爵
        //徐志成
        //王振文
        //赵海江
        //宋莉明
        //陈莉花
        //龚文杰
        //裴钦泗
        //刘森


//        Kw7nOp0QkKI=
//                8g23PKxUJ5FO0DaCTfzKqg==
//                1_e21tjyjj730pUl4XAV_Q==
        //.K_0PrIkZZWCOwVSdQ.aBfcTtxeKanzJ
        //MEy5C7dZb4kKRAyaaVarQ70zsHh4qDLW
        //Ud4nnEKzmZvPZcfFTBVmNefD0kYsQLIq

        Logger logger = LoggerFactory.getLogger(HyTreeDesUtil.class);
        String cipherPwd = "853467199003214683";
//        "4297f44b13955235245b2497399d7a93"

        // 解密密码
        byte[] bytes = HyTreeDesPKCPaddingUtil.des3DecodeCBC(HyUtil.FIX_KEY, HyUtil.keyiv, HyTreeDesECBUtils.hexStringToBytes("84FDD11776D49C34"));
        String des3DecodePass = new String(bytes, "UTF-8");
        System.out.println(des3DecodePass);

        String[] arr={"1DC9F327D4D9D9A3"};


        System.out.println(new String(HyTreeDesPKCPaddingUtil.des3DecodeCBC(HyUtil.FIX_KEY, HyUtil.keyiv, HyTreeDesECBUtils.hexStringToBytes("A4554A22B31004590C496352326CED05"))));

//        newPassword = HyTreeDesECBUtils.byte2HexStr(HyTreeDesPKCPaddingUtil.des3EncodeCBC(HyUtil.FIX_KEY, HyUtil.keyiv, decodePassword.getBytes(StandardCharsets.UTF_8)));
//        for (String s : arr) {
//            System.out.println(s+" = "+getDes3DecodeCBC(s, LoggerFactory.getLogger(HyTreeDesUtil.class)));
//        }

        System.out.println(getDes3EncodeCBC("430802200105286619",LoggerFactory.getLogger(HyTreeDesUtil.class)));
        System.out.println(getDes3EncodeCBC("18558728781",LoggerFactory.getLogger(HyTreeDesUtil.class)));
    }
}